package com.zhongwang.mms.module.buy.listner;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.mms.common.ImportCallBack;
import com.zhongwang.mms.gen.entity.BReportPlan;
import com.zhongwang.mms.gen.entity.BReportPlanChange;
import com.zhongwang.mms.gen.entity.TDictionary;
import com.zhongwang.mms.gen.entity.TEmploy;
import com.zhongwang.mms.gen.mapper.BReportPlanChangeMapper;
import com.zhongwang.mms.gen.mapper.BReportPlanMapper;
import com.zhongwang.mms.module.buy.dao.ReportPlanDao;
import com.zhongwang.mms.module.buy.model.*;
import com.zhongwang.mms.util.BigDecimalUtils;
import com.zhongwang.mms.util.CodeUtil;
import com.zhongwang.mms.util.ShiroUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.zhongwang.mms.util.BeanUtil.copy;

/**
 * @Description: 提报计划导入并修改 listener
 * @Author: China.wsg
 * @Date: 2020/01/09 16:38
 */
public class EditReportPlanListener extends AnalysisEventListener<ExportDeferralModel> {

    private ReportPlanDao reportPlanDao;
    private ImportCallBack callBack;
    private List<ExportDeferralModel> allDataList = new ArrayList<>();
    private List<ExportDeferralModel> dataList = new ArrayList<>();
    private List<ExportDeferralErrorLogModel> errorList = new ArrayList<>();
    private BReportPlanChangeMapper bReportPlanChangeMapper;
    private List<Integer> exceptionIndexList = new ArrayList<>();
    //更改过数据的计划
    private List<Integer> changeList = new ArrayList<Integer>();
    private int dataIndex = 0;
    /**
     * @name: operationTime
     * @description: 操作时间
     * @author: 徐一贺
     * @coddingDate: 2020/8/21 13:26
     * @version: v1.0
     **/
    private Date operationTime;
    /**
     * @name: operatorId
     * @description: 操作用户主键
     * @author: 徐一贺
     * @coddingDate: 2020/8/21 13:26
     * @version: v1.0
     **/
    private Integer operatorId;

    public EditReportPlanListener(
            ReportPlanDao reportPlanDao,
            ImportCallBack callBack,
            BReportPlanChangeMapper bReportPlanChangeMapper,
            Integer operatorId) {
        this.reportPlanDao = reportPlanDao;
        this.callBack = callBack;
        this.bReportPlanChangeMapper = bReportPlanChangeMapper;
        this.operationTime = new Date();
        this.operatorId = operatorId;
    }

    /**
     * @name: invoke
     * @description: 第一次过滤验证和获取第二次过滤验证需要的数据
     * @param: [data, context]
     * @returnType: void
     * @author: 徐一贺
     * @coddingDate: 2020/8/20 15:44
     * @version: v1.0
     **/
    @Override
    public void invoke(ExportDeferralModel data, AnalysisContext context) {
        allDataList.add(data);
        String errorInfo = checkNum(data);
        if (errorInfo.equals("")) {
            dataList.add(data);
        } else {
            recordErrorModel(data, errorInfo);
        }
        dataIndex++;
    }

    /**
     * @name: doAfterAllAnalysed
     * @description: 开始执行二次过滤验证和操作
     * @param: [context]
     * @returnType: void
     * @author: 徐一贺
     * @coddingDate: 2020/8/21 11:08
     * @version: v1.0
     **/
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        try {
            if (exceptionIndexList.size() > 0) {
                for (int i = 0; i < exceptionIndexList.size(); i++) {
                    recordErrorModel(allDataList.get(exceptionIndexList.get(i)), "数据格式不正确，不要修改禁改数据！");
                }
            }
            //获取二次验证所有计划列表
            List<ExportDeferralModel> secondaryFiltrationData = dataList;
            //获取依赖父级的ID
            for (ExportDeferralModel exportDeferralModel : dataList) {
                exportDeferralModel.setRppSrcId(reportPlanDao.getById(exportDeferralModel.getRppId()).getRppSrcId());
            }
            //开始二次验证 筛选掉因父级错误一想到子级不能操作的
            if (errorList.size() > 0) {
                for (ExportDeferralModel secondaryFiltrationModel : secondaryFiltrationData) {
                    BReportPlan bReportPlan = reportPlanDao.getById(secondaryFiltrationModel.getRppId());
                    if (bReportPlan.getRppSrcId() != null) {
                        boolean isNotError = true;
                        for (ExportDeferralErrorLogModel errorLogModel : errorList) {
                            if (bReportPlan.getRppSrcId().equals(errorLogModel.getRppId())) {
                                isNotError = false;
                                recordErrorModel(secondaryFiltrationModel, "因其原计划或旧计划出现错误，故无法操作：" + errorLogModel.getErrorInfo());
                                break;
                            }
                        }
                    }
                }
            }
            //获取通过二次验证的三次筛选计划
            List<ExportDeferralModel> tertiaryFiltrationData = new ArrayList<ExportDeferralModel>();
            //通过循环获取三次筛选的数据
            for (ExportDeferralModel model : secondaryFiltrationData) {
                boolean isNotError = true;
                for (ExportDeferralErrorLogModel errorLogModel : errorList) {
                    if (model.getRppId().equals(errorLogModel.getRppId())) {
                        isNotError = false;
                        break;
                    }
                }
                if (isNotError) {
                    tertiaryFiltrationData.add(model);
                }
            }
            //原计划
            List<ExportDeferralModel> sourceList = new ArrayList<ExportDeferralModel>();
            //旧计划
            List<ExportDeferralModel> oldList = new ArrayList<ExportDeferralModel>();
            //新计划
            List<ExportDeferralModel> newList = new ArrayList<ExportDeferralModel>();


            //进行第三次筛选 本次筛选的目标验证 正常提报数量 使用库存数量 暂缓数量是否与数据库相同
            for (ExportDeferralModel exportDeferralModel : tertiaryFiltrationData) {
                //获取其子级正常提报模型
                BReportPlan normalReportPlan = reportPlanDao.getBReportPlanByIdAndRppUseStock(exportDeferralModel.getRppId(), 2);
                //获取其子级使用库存模型
                BReportPlan useStockReportPlan = reportPlanDao.getBReportPlanByIdAndRppUseStock(exportDeferralModel.getRppId(), 1);
                //获取其子级暂缓模型
                BReportPlan postponeReportPlan = reportPlanDao.getBReportPlanByIdAndRppUseStock(exportDeferralModel.getRppId(), 3);
                //获取其子级甲供模型
                BReportPlan providedByPartyAReportPlan = reportPlanDao.getBReportPlanByIdAndRppUseStock(exportDeferralModel.getRppId(), 4);


                if (null != normalReportPlan && !normalReportPlan.getRppNum().equals(exportDeferralModel.getNormalNum())) {
                    for (ExportDeferralModel child : tertiaryFiltrationData) {
                        if (child.getRppId().equals(normalReportPlan.getRppId())) {
                            child.setRppSrcId(exportDeferralModel.getRppId());
                        }
                    }
                    exportDeferralModel.setSyncFlag(false);
                } else if (null == normalReportPlan && exportDeferralModel.getNormalNum() != null && exportDeferralModel.getNormalNum() != 0) {
                    exportDeferralModel.setSyncFlag(false);
                }
                if (null != useStockReportPlan && !useStockReportPlan.getRppNum().equals(exportDeferralModel.getUseStockNum())) {
                    for (ExportDeferralModel child : tertiaryFiltrationData) {
                        if (child.getRppId().equals(useStockReportPlan.getRppId())) {
                            child.setRppSrcId(exportDeferralModel.getRppId());
                        }
                    }
                    exportDeferralModel.setSyncFlag(false);
                } else if (null == useStockReportPlan && exportDeferralModel.getUseStockNum() != null && exportDeferralModel.getUseStockNum() != 0) {
                    exportDeferralModel.setSyncFlag(false);
                }
                if (null != postponeReportPlan && !postponeReportPlan.getRppNum().equals(exportDeferralModel.getPostponeNum())) {
                    for (ExportDeferralModel child : tertiaryFiltrationData) {
                        if (child.getRppId().equals(postponeReportPlan.getRppId())) {
                            child.setRppSrcId(exportDeferralModel.getRppId());
                        }
                    }
                    exportDeferralModel.setSyncFlag(false);
                } else if (null == postponeReportPlan && exportDeferralModel.getPostponeNum() != null && exportDeferralModel.getPostponeNum() != 0) {
                    exportDeferralModel.setSyncFlag(false);
                }
                if (null != providedByPartyAReportPlan && !providedByPartyAReportPlan.getRppNum().equals(exportDeferralModel.getPostponeNum())) {
                    for (ExportDeferralModel child : tertiaryFiltrationData) {
                        if (child.getRppId().equals(providedByPartyAReportPlan.getRppId())) {
                            child.setRppSrcId(exportDeferralModel.getRppId());
                        }
                    }
                    exportDeferralModel.setSyncFlag(false);
                } else if (null == providedByPartyAReportPlan && exportDeferralModel.getPostponeNum() != null && exportDeferralModel.getPostponeNum() != 0) {
                    exportDeferralModel.setSyncFlag(false);
                }

                //把计划池分配到原计划、旧计划和新计划列表
                BReportPlan plan = reportPlanDao.getById(exportDeferralModel.getRppId());
                switch (plan.getRppSplitPlanType()) {
                    case "old":
                        oldList.add(exportDeferralModel);
                        break;
                    case "new":
                        newList.add(exportDeferralModel);
                        break;
                    default:
                        sourceList.add(exportDeferralModel);
                        break;
                }
                List<ExportDeferralModel> fourTimesFiltration = new ArrayList<ExportDeferralModel>();
                //通过遍历原计划在旧计划和新计划中找到对应的拆分出来的计划，同时验证是否需要修改，如果需要修改数量，如果父子级都需要修改则设置成错误数据
                for (ExportDeferralModel source : sourceList) {
                    if (source.getSyncFlag() == false) {
                        String errorInfo = "";
                        for (ExportDeferralModel oldModel : oldList) {
                            if (oldModel.getRppSrcId().equals(source.getRppId()) && oldModel.getSyncFlag() == false) {
                                errorInfo += "此计划的原计划于当前计划的正常提报数量、使用库存数量、暂缓数量、甲供数量不可同时修改！只能修改原计划或此计划的数量！";
                                recordErrorModel(oldModel, errorInfo);
                            }
                        }
                        for (ExportDeferralModel newModel : newList) {
                            if (newModel.getRppSrcId().equals(source.getRppId()) && newModel.getSyncFlag() == false) {
                                errorInfo += "此计划的原计划于当前计划的正常提报数量、使用库存数量、暂缓数量、甲供数量不可同时修改！只能修改原计划或此计划的数量！";
                                recordErrorModel(newModel, errorInfo);
                            }
                        }
                    }
                }
                //通过遍历旧计划在新计划中找到对应的拆分出来的计划，同时验证是否需要修改，如果需要修改数量，如果父子级都需要修改则设置成错误数据
                for (ExportDeferralModel oldModel : oldList) {
                    if (oldModel.getSyncFlag() == false) {
                        String errorInfo = "";
                        for (ExportDeferralModel newModel : newList) {
                            if (newModel.getRppSrcId().equals(oldModel.getRppId()) && newModel.getSyncFlag() == false) {
                                errorInfo += "此计划的原计划于当前计划的正常提报数量、使用库存数量、暂缓数量、甲供数量不可同时修改！只能修改原计划或此计划的数量！";
                                recordErrorModel(newModel, errorInfo);
                            }
                        }
                    }
                }
                //子孙计划
                List<ExportDeferralModel> descendantsList = new ArrayList<ExportDeferralModel>();
                descendantsList.addAll(oldList);
                descendantsList.addAll(newList);
                //通过遍历拆分出来的子孙计划，同时验证是否需要修改，如果需要修改数量，如果父子级都需要修改则设置成错误数据
                for (ExportDeferralModel source : sourceList) {
                    for (ExportDeferralModel descendants : descendantsList) {
                        BReportPlan bReportPlan = reportPlanDao.getById(descendants.getRppId());
                        String errorInfo = "";
                        if (bReportPlan.getRppAncestorsId().equals(source.getRppId()) && descendants.getSyncFlag() == false) {
                            int notSameCount = 0;
                            Integer srcId = null;
                            List<BReportPlan> allBReportPlan = reportPlanDao.findReportPlanDaoByRppAncestorsId(source.getRppId());
                            for (int i = 0; i < allBReportPlan.size(); i++) {
                                for (int j = 0; j < tertiaryFiltrationData.size(); j++) {
                                    if (allBReportPlan.get(i).getRppId().equals(tertiaryFiltrationData.get(j).getRppId()) && !allBReportPlan.get(i).getRppNum().equals(tertiaryFiltrationData.get(j).getRppNum())) {
                                        BReportPlan com = reportPlanDao.getById(tertiaryFiltrationData.get(j).getRppId());
                                        if (com.getRppSrcId() != null && srcId == null) {
                                            srcId = com.getRppSrcId();
                                        }
                                        if (srcId != null && !srcId.equals(com.getRppSrcId())) {
                                            notSameCount++;
                                        }
                                        break;
                                    }
                                }
                            }
                            if (notSameCount > 1) {
                                errorInfo += "此计划的原计划于当前计划的正常提报数量、使用库存数量、暂缓数量、甲供数量不可同时修改！只能修改原计划或此计划的数量！";
                                recordErrorModel(descendants, errorInfo);
                            }
                        }
                    }
                }
                //获取可以操作的原计划
                List<ExportDeferralModel> operSourceList = new ArrayList<ExportDeferralModel>();
                //获取可以操作的旧计划
                List<ExportDeferralModel> operOldList = new ArrayList<ExportDeferralModel>();
                //获取可以操作的新计划
                List<ExportDeferralModel> operNewList = new ArrayList<ExportDeferralModel>();
                for (ExportDeferralModel sourceModel : sourceList) {
                    boolean isNotError = true;
                    for (ExportDeferralErrorLogModel error : errorList) {
                        if (sourceModel.getRppId().equals(error.getRppId())) {
                            isNotError = false;
                        }
                    }
                    if (isNotError) {
                        operSourceList.add(sourceModel);
                    }
                }
                for (ExportDeferralModel oldModel : oldList) {
                    boolean isNotError = true;
                    for (ExportDeferralErrorLogModel error : errorList) {
                        if (oldModel.getRppId().equals(error.getRppId())) {
                            isNotError = false;
                        }
                    }
                    if (isNotError) {
                        operOldList.add(oldModel);
                    }
                }
                for (ExportDeferralModel newModel : newList) {
                    boolean isNotError = true;
                    for (ExportDeferralErrorLogModel error : errorList) {
                        if (newModel.getRppId().equals(error.getRppId())) {
                            isNotError = false;
                        }
                    }
                    if (isNotError) {
                        operNewList.add(newModel);
                    }
                }
                operSourceList.addAll(operOldList);
                operSourceList.addAll(operNewList);
                //开始执行拆分
                //1.原计划拆分如果有对应的子级计划则更新对应的子级计划数量，没有对应的子级计划则新增子级计划
                for (ExportDeferralModel sourceModel : operSourceList) {
                    boolean isNotHave = true;
                    for (Integer change : changeList) {
                        if (change.equals(sourceModel.getRppId())) {
                            isNotHave = false;
                            break;
                        }
                    }
                    if (isNotHave) {
                        if (sourceModel.getSyncFlag() == false) {
                            BReportPlan sourcePlan = reportPlanDao.getById(sourceModel.getRppId());
                            if (sourcePlan != null && sourceModel.getRppRemarkMatDesc() != null) {
                                sourcePlan.setRppRemarkMatDesc(sourceModel.getRppRemarkMatDesc());
                            }
                            if (sourcePlan != null && sourceModel.getRppRemarkSapCode() != null) {
                                sourcePlan.setRppRemarkSapCode(sourceModel.getRppRemarkSapCode());
                            }
                            //获取其子级正常提报模型
                            BReportPlan normal = reportPlanDao.getBReportPlanByIdAndRppUseStock(exportDeferralModel.getRppId(), 2);
                            if (normal != null && sourceModel.getRppRemarkMatDesc() != null) {
                                normal.setRppRemarkMatDesc(sourceModel.getRppRemarkMatDesc());
                            }
                            if (normal != null && sourceModel.getRppRemarkSapCode() != null) {
                                normal.setRppRemarkSapCode(sourceModel.getRppRemarkSapCode());
                            }
                            //获取其子级使用库存模型
                            BReportPlan useStock = reportPlanDao.getBReportPlanByIdAndRppUseStock(exportDeferralModel.getRppId(), 1);
                            if (useStock != null && sourceModel.getRppRemarkMatDesc() != null) {
                                useStock.setRppRemarkMatDesc(sourceModel.getRppRemarkMatDesc());
                            }
                            if (useStock != null && sourceModel.getRppRemarkSapCode() != null) {
                                useStock.setRppRemarkSapCode(sourceModel.getRppRemarkSapCode());
                            }
                            //获取其子级暂缓模型
                            BReportPlan postpone = reportPlanDao.getBReportPlanByIdAndRppUseStock(exportDeferralModel.getRppId(), 3);
                            if (postpone != null && sourceModel.getRppRemarkMatDesc() != null) {
                                postpone.setRppRemarkMatDesc(sourceModel.getRppRemarkMatDesc());
                            }
                            if (postpone != null && sourceModel.getRppRemarkSapCode() != null) {
                                postpone.setRppRemarkSapCode(sourceModel.getRppRemarkSapCode());
                            }
                            //获取其子级甲供模型
                            BReportPlan providedByPartyA = reportPlanDao.getBReportPlanByIdAndRppUseStock(exportDeferralModel.getRppId(), 3);
                            if (providedByPartyA != null && sourceModel.getRppRemarkMatDesc() != null) {
                                providedByPartyA.setRppRemarkMatDesc(sourceModel.getRppRemarkMatDesc());
                            }
                            if (providedByPartyA != null && sourceModel.getRppRemarkSapCode() != null) {
                                providedByPartyA.setRppRemarkSapCode(sourceModel.getRppRemarkSapCode());
                            }
                            sourcePlan = reportPlanDao.getById(sourceModel.getRppId());
                            if (normal != null &&
                                    useStock != null &&
                                    sourceModel.getNormalNum() != null &&
                                    sourceModel.getNormalNum() == 0 &&
                                    sourceModel.getUseStockNum() != null &&
                                    sourceModel.getUseStockNum() == 0 &&
                                    sourceModel.getPostponeNum() != null &&
                                    sourceModel.getPostponeNum() == 0 &&
                                    sourceModel.getProvidedByPartyANum() != null &&
                                    sourceModel.getProvidedByPartyANum() == 0
                            ) {
                                boolean isNotHaveSource = true;
                                for (Integer change : changeList) {
                                    if (change.equals(sourceModel.getRppId())) {
                                        isNotHaveSource = false;
                                        break;
                                    }
                                    if (change.equals(normal.getRppId())) {
                                        isNotHaveSource = false;
                                        break;
                                    }
                                    if (change.equals(useStock.getRppId())) {
                                        isNotHaveSource = false;
                                        break;
                                    }
                                    if (change.equals(postpone.getRppId())) {
                                        isNotHaveSource = false;
                                        break;
                                    }
                                    if (change.equals(providedByPartyA.getRppId())) {
                                        isNotHaveSource = false;
                                        break;
                                    }
                                }
                                if (isNotHaveSource) {
                                    if (sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                        sourcePlan.setRppSplitPlanType("source");
                                        sourcePlan.setRppOriginalFlag(1);
                                    } else if (sourcePlan.getRppSplitPlanType().equals("old")) {
                                        sourcePlan.setRppSplitPlanType("new");
                                        sourcePlan.setRppOriginalFlag(2);
                                    }
                                    reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                    normal.setRppNum(0d);
                                    normal.setRppChangeMark(1);
                                    reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(normal);
                                    reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(normal);
                                    changeList.add(normal.getRppId());
                                    List<BReportPlan> normalList = reportPlanDao.findBReportPlanByRppSrcId(normal.getRppId());
                                    for (BReportPlan model : normalList) {
                                        updateDescendantsRppNumByRppId(model);
                                    }

                                    useStock.setRppNum(0d);
                                    useStock.setRppChangeMark(1);
                                    reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(useStock);
                                    reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(useStock);
                                    changeList.add(useStock.getRppId());
                                    List<BReportPlan> useStocklList = reportPlanDao.findBReportPlanByRppSrcId(useStock.getRppId());
                                    for (BReportPlan model : useStocklList) {
                                        updateDescendantsRppNumByRppId(model);
                                    }

                                    postpone.setRppNum(0d);
                                    postpone.setRppChangeMark(1);
                                    reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(postpone);
                                    reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(postpone);
                                    changeList.add(postpone.getRppId());
                                    List<BReportPlan> postponeList = reportPlanDao.findBReportPlanByRppSrcId(postpone.getRppId());
                                    for (BReportPlan model : postponeList) {
                                        updateDescendantsRppNumByRppId(model);
                                    }
                                }
                            } else {
                                if (null != sourceModel.getNormalNum() && sourceModel.getNormalNum() != 0 && sourceModel.getNormalNum().equals(sourcePlan.getRppNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                            sourcePlan.setRppSplitPlanType("source");
                                            sourcePlan.setRppOriginalFlag(1);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("old")) {
                                            sourcePlan.setRppSplitPlanType("new");
                                            sourcePlan.setRppOriginalFlag(2);
                                        }
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        sourcePlan.setRppNum(sourceModel.getNormalNum());
                                        sourcePlan.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(sourcePlan);
                                        sourcePlan.setRppUseStock(2);
                                        reportPlanDao.updateBReportPlanRppUseStockByRppId(sourcePlan);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(sourcePlan);
                                        List<BReportPlan> modelList = reportPlanDao.findBReportPlanByRppSrcId(sourcePlan.getRppId());
                                        for (BReportPlan model : modelList) {
                                            updateDescendantsRppNumByRppId(model);
                                        }
                                        changeList.add(sourcePlan.getRppId());
                                    }
                                } else if (null != normal && sourceModel.getNormalNum() != null && sourceModel.getNormalNum() != 0 && !normal.getRppNum().equals(sourceModel.getNormalNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (sourcePlan.getRppSplitPlanType().equals("source")) {
                                            sourcePlan.setRppSplitPlanType("oldSource");
                                            sourcePlan.setRppOriginalFlag(3);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("new")) {
                                            sourcePlan.setRppSplitPlanType("old");
                                            sourcePlan.setRppOriginalFlag(3);
                                        }

                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        normal.setRppNum(sourceModel.getNormalNum());
                                        normal.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(normal);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(normal);
                                        changeList.add(normal.getRppId());
                                    }
                                } else if (null != normal && sourceModel.getNormalNum() != 0 && !normal.getRppNum().equals(sourceModel.getNormalNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (sourcePlan.getRppSplitPlanType().equals("source")) {
                                            sourcePlan.setRppSplitPlanType("oldSource");
                                            sourcePlan.setRppOriginalFlag(3);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("new")) {
                                            sourcePlan.setRppSplitPlanType("old");
                                            sourcePlan.setRppOriginalFlag(3);
                                        }

                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        normal.setRppNum(sourceModel.getNormalNum());
                                        normal.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(normal);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(normal);
                                        changeList.add(normal.getRppId());
                                        updateDescendantsRppNumByRppId(normal);
                                    }
                                } else if (null != normal && sourceModel.getNormalNum() == 0 && !normal.getRppNum().equals(sourceModel.getNormalNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (normal.getRppSplitPlanType().equals("oldSource")) {
                                            normal.setRppSplitPlanType("source");
                                            normal.setRppOriginalFlag(1);
                                        } else if (normal.getRppSplitPlanType().equals("old")) {
                                            normal.setRppSplitPlanType("new");
                                            normal.setRppOriginalFlag(2);
                                        }
                                        normal.setRppNum(sourceModel.getNormalNum());
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(normal);
                                        normal.setRppNum(0d);
                                        normal.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(normal);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(normal);
                                        changeList.add(normal.getRppId());
                                        updateDescendantsRppNumByRppId(normal);
                                    }
                                } else if (null == normal && sourceModel.getNormalNum() != 0 && !sourceModel.getNormalNum().equals(reportPlanDao.getById(sourceModel.getRppId()).getRppNum())) {
                                    //插入新拆分的数据
                                    normal = reportPlanDao.getBReportPlanByIdAndRppUseStock(sourcePlan.getRppId(), 2);
                                    if (normal == null) {
                                        if (sourcePlan.getRppSplitPlanType().equals("source") || sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                            sourcePlan.setRppOriginalFlag(3);
                                            sourcePlan.setRppSplitPlanType("oldSource");
                                        } else if (sourcePlan.getRppSplitPlanType().equals("old") || sourcePlan.getRppSplitPlanType().equals("new")) {
                                            sourcePlan.setRppOriginalFlag(3);
                                            sourcePlan.setRppSplitPlanType("old");
                                        }
                                        //更新原计划拆分标识与拆分身份
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        changeList.add(sourceModel.getRppId());
                                        sourcePlan.setRppNum(sourceModel.getNormalNum());
                                        sourcePlan.setRppOriginalFlag(2);
                                        sourcePlan.setRppSplitPlanType("new");
                                        sourcePlan.setRppUseStock(2);
                                        if (sourcePlan.getRppAncestorsId() == null) {
                                            sourcePlan.setRppAncestorsId(sourcePlan.getRppId());
                                        }
                                        sourcePlan.setRppSrcId(sourceModel.getRppId());

                                        reportPlanDao.insertEntity(sourcePlan);
                                        changeList.add(sourcePlan.getRppId());
                                    }


                                } else if (null == normal && sourceModel.getNormalNum() != 0 && sourceModel.getNormalNum().equals(reportPlanDao.getById(sourceModel.getRppId()).getRppNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        BReportPlan bReportPlan = reportPlanDao.getById(sourceModel.getRppId());
                                        bReportPlan.setRppUseStock(2);
                                        //更新原计划提报类型
                                        reportPlanDao.updateBReportPlanRppUseStockByRppId(bReportPlan);
                                        if (sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                            sourcePlan.setRppSplitPlanType("source");
                                            sourcePlan.setRppOriginalFlag(1);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("old")) {
                                            sourcePlan.setRppSplitPlanType("new");
                                            sourcePlan.setRppOriginalFlag(2);
                                        }
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(sourcePlan);
                                        changeList.add(sourcePlan.getRppId());
                                        List<BReportPlan> modelList = reportPlanDao.findBReportPlanByRppSrcId(sourcePlan.getRppId());
                                        for (BReportPlan model : modelList) {
                                            updateDescendantsRppNumByRppId(model);
                                        }
                                    }
                                }
                                sourcePlan = reportPlanDao.getById(sourceModel.getRppId());

                                if (null != sourceModel.getUseStockNum() && sourceModel.getUseStockNum() != 0 && sourceModel.getUseStockNum().equals(sourcePlan.getRppNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                            sourcePlan.setRppSplitPlanType("source");
                                            sourcePlan.setRppOriginalFlag(1);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("old")) {
                                            sourcePlan.setRppSplitPlanType("new");
                                            sourcePlan.setRppOriginalFlag(2);
                                        }
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        sourcePlan.setRppNum(sourceModel.getUseStockNum());
                                        sourcePlan.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(sourcePlan);
                                        sourcePlan.setRppUseStock(1);
                                        reportPlanDao.updateBReportPlanRppUseStockByRppId(sourcePlan);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(sourcePlan);
                                        changeList.add(sourcePlan.getRppId());
                                        List<BReportPlan> modelList = reportPlanDao.findBReportPlanByRppSrcId(sourcePlan.getRppId());
                                        for (BReportPlan model : modelList) {
                                            updateDescendantsRppNumByRppId(model);
                                        }
                                    }
                                } else if (null != useStock && sourceModel.getUseStockNum() != null && sourceModel.getUseStockNum() != 0 && !useStock.getRppNum().equals(sourceModel.getUseStockNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (sourcePlan.getRppSplitPlanType().equals("source")) {
                                            sourcePlan.setRppSplitPlanType("oldSource");
                                            sourcePlan.setRppOriginalFlag(3);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("new")) {
                                            sourcePlan.setRppSplitPlanType("old");
                                            sourcePlan.setRppOriginalFlag(3);
                                        }

                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        useStock.setRppNum(sourceModel.getUseStockNum());
                                        useStock.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(useStock);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(useStock);
                                        changeList.add(useStock.getRppId());
                                    }
                                } else if (null != useStock && sourceModel.getUseStockNum() != 0 && !useStock.getRppNum().equals(sourceModel.getUseStockNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (sourcePlan.getRppSplitPlanType().equals("source")) {
                                            sourcePlan.setRppSplitPlanType("oldSource");
                                            sourcePlan.setRppOriginalFlag(3);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("new")) {
                                            sourcePlan.setRppSplitPlanType("old");
                                            sourcePlan.setRppOriginalFlag(3);
                                        }
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        useStock.setRppNum(sourceModel.getUseStockNum());
                                        useStock.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(useStock);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(useStock);
                                        changeList.add(useStock.getRppId());
                                        updateDescendantsRppNumByRppId(useStock);
                                    }
                                } else if (null != useStock && sourceModel.getUseStockNum() == 0 && !useStock.getRppNum().equals(sourceModel.getUseStockNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (useStock.getRppSplitPlanType().equals("oldSource")) {
                                            useStock.setRppSplitPlanType("source");
                                            useStock.setRppOriginalFlag(1);
                                        } else if (useStock.getRppSplitPlanType().equals("old")) {
                                            useStock.setRppSplitPlanType("new");
                                            useStock.setRppOriginalFlag(2);
                                        }
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(useStock);
                                        useStock.setRppNum(0d);
                                        useStock.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(useStock);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(useStock);
                                        changeList.add(useStock.getRppId());
                                        updateDescendantsRppNumByRppId(useStock);
                                    }
                                } else if (null == useStock && sourceModel.getUseStockNum() != 0 && !sourceModel.getUseStockNum().equals(reportPlanDao.getById(sourceModel.getRppId()).getRppNum())) {
                                    //插入新拆分的数据
                                    useStock = reportPlanDao.getBReportPlanByIdAndRppUseStock(sourcePlan.getRppId(), 1);
                                    if (useStock == null) {
                                        if (sourcePlan.getRppSplitPlanType().equals("source") || sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                            sourcePlan.setRppOriginalFlag(3);
                                            sourcePlan.setRppSplitPlanType("oldSource");
                                        } else if (sourcePlan.getRppSplitPlanType().equals("old") || sourcePlan.getRppSplitPlanType().equals("new")) {
                                            sourcePlan.setRppOriginalFlag(3);
                                            sourcePlan.setRppSplitPlanType("old");
                                        }
                                        //更新原计划拆分标识与拆分身份
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        changeList.add(sourceModel.getRppId());
                                        sourcePlan.setRppNum(sourceModel.getUseStockNum());
                                        sourcePlan.setRppOriginalFlag(2);
                                        sourcePlan.setRppSplitPlanType("new");
                                        sourcePlan.setRppUseStock(1);
                                        if (sourcePlan.getRppAncestorsId() == null) {
                                            sourcePlan.setRppAncestorsId(sourcePlan.getRppId());
                                        }
                                        sourcePlan.setRppSrcId(sourceModel.getRppId());

                                        reportPlanDao.insertEntity(sourcePlan);

                                        changeList.add(sourcePlan.getRppId());
                                    }
                                } else if (null == useStock && sourceModel.getUseStockNum() != 0 && sourceModel.getUseStockNum().equals(reportPlanDao.getById(sourceModel.getRppId()).getRppNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        BReportPlan bReportPlan = reportPlanDao.getById(sourceModel.getRppId());
                                        bReportPlan.setRppUseStock(1);
                                        //更新原计划提报类型
                                        reportPlanDao.updateBReportPlanRppUseStockByRppId(bReportPlan);
                                        if (sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                            sourcePlan.setRppSplitPlanType("source");
                                            sourcePlan.setRppOriginalFlag(1);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("old")) {
                                            sourcePlan.setRppSplitPlanType("new");
                                            sourcePlan.setRppOriginalFlag(2);
                                        }
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(sourcePlan);
                                        changeList.add(sourcePlan.getRppId());
                                        List<BReportPlan> modelList = reportPlanDao.findBReportPlanByRppSrcId(sourcePlan.getRppId());
                                        for (BReportPlan model : modelList) {
                                            updateDescendantsRppNumByRppId(model);
                                        }
                                    }
                                }
                                sourcePlan = reportPlanDao.getById(sourceModel.getRppId());

                                if (null != sourceModel.getPostponeNum() && sourceModel.getPostponeNum() != 0 && sourceModel.getPostponeNum().equals(sourcePlan.getRppNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                            sourcePlan.setRppSplitPlanType("source");
                                            sourcePlan.setRppOriginalFlag(1);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("old")) {
                                            sourcePlan.setRppSplitPlanType("new");
                                            sourcePlan.setRppOriginalFlag(2);
                                        }
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        sourcePlan.setRppNum(sourceModel.getPostponeNum());
                                        sourcePlan.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(sourcePlan);
                                        sourcePlan.setRppUseStock(3);
                                        reportPlanDao.updateBReportPlanRppUseStockByRppId(sourcePlan);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(sourcePlan);
                                        changeList.add(sourcePlan.getRppId());
                                        List<BReportPlan> modelList = reportPlanDao.findBReportPlanByRppSrcId(sourcePlan.getRppId());
                                        for (BReportPlan model : modelList) {
                                            updateDescendantsRppNumByRppId(model);
                                        }
                                    }
                                } else if (null != postpone && sourceModel.getPostponeNum() != null && sourceModel.getPostponeNum() != 0 && !postpone.getRppNum().equals(sourceModel.getPostponeNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (sourcePlan.getRppSplitPlanType().equals("source")) {
                                            sourcePlan.setRppSplitPlanType("oldSource");
                                            sourcePlan.setRppOriginalFlag(3);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("new")) {
                                            sourcePlan.setRppSplitPlanType("old");
                                            sourcePlan.setRppOriginalFlag(3);
                                        }

                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        postpone.setRppNum(sourceModel.getPostponeNum());
                                        postpone.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(postpone);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(postpone);
                                        changeList.add(postpone.getRppId());
                                    }
                                } else if (null != postpone && sourceModel.getPostponeNum() != 0 && !postpone.getRppNum().equals(sourceModel.getPostponeNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (sourcePlan.getRppSplitPlanType().equals("source")) {
                                            sourcePlan.setRppSplitPlanType("oldSource");
                                            sourcePlan.setRppOriginalFlag(3);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("new")) {
                                            sourcePlan.setRppSplitPlanType("old");
                                            sourcePlan.setRppOriginalFlag(3);
                                        }

                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(postpone);
                                        postpone.setRppNum(sourceModel.getPostponeNum());
                                        postpone.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(postpone);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(postpone);
                                        changeList.add(postpone.getRppId());
                                        updateDescendantsRppNumByRppId(postpone);
                                    }
                                } else if (null != postpone && sourceModel.getPostponeNum() == 0 && !postpone.getRppNum().equals(sourceModel.getPostponeNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (postpone.getRppSplitPlanType().equals("oldSource")) {
                                            postpone.setRppSplitPlanType("source");
                                            postpone.setRppOriginalFlag(1);
                                        } else if (postpone.getRppSplitPlanType().equals("old")) {
                                            postpone.setRppSplitPlanType("new");
                                            postpone.setRppOriginalFlag(2);
                                        }
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(postpone);
                                        postpone.setRppNum(0d);
                                        postpone.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(postpone);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(postpone);
                                        changeList.add(postpone.getRppId());
                                        updateDescendantsRppNumByRppId(postpone);
                                    }
                                } else if (null == postpone && sourceModel.getPostponeNum() != 0 && !sourceModel.getPostponeNum().equals(reportPlanDao.getById(sourceModel.getRppId()).getRppNum())) {
                                    postpone = reportPlanDao.getBReportPlanByIdAndRppUseStock(sourcePlan.getRppId(), 3);
                                    if (postpone == null) {
                                        if (sourcePlan.getRppSplitPlanType().equals("source") || sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                            sourcePlan.setRppOriginalFlag(3);
                                            sourcePlan.setRppSplitPlanType("oldSource");
                                        } else if (sourcePlan.getRppSplitPlanType().equals("old") || sourcePlan.getRppSplitPlanType().equals("new")) {
                                            sourcePlan.setRppOriginalFlag(3);
                                            sourcePlan.setRppSplitPlanType("old");
                                        }
                                        //更新原计划拆分标识与拆分身份
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        changeList.add(sourceModel.getRppId());
                                        sourcePlan.setRppNum(sourceModel.getPostponeNum());
                                        sourcePlan.setRppOriginalFlag(2);
                                        sourcePlan.setRppSplitPlanType("new");
                                        sourcePlan.setRppUseStock(3);
                                        if (sourcePlan.getRppAncestorsId() == null) {
                                            sourcePlan.setRppAncestorsId(sourcePlan.getRppId());
                                        }
                                        sourcePlan.setRppSrcId(sourceModel.getRppId());
                                        //插入新拆分的数据

                                        //插入新拆分的数据
                                        reportPlanDao.insertEntity(sourcePlan);
                                        changeList.add(sourcePlan.getRppId());
                                    }
                                } else if (null == postpone && sourceModel.getPostponeNum() != 0 && sourceModel.getPostponeNum().equals(reportPlanDao.getById(sourceModel.getRppId()).getRppNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        BReportPlan bReportPlan = reportPlanDao.getById(sourceModel.getRppId());
                                        bReportPlan.setRppUseStock(3);
                                        //更新原计划提报类型
                                        reportPlanDao.updateBReportPlanRppUseStockByRppId(bReportPlan);
                                        if (sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                            sourcePlan.setRppSplitPlanType("source");
                                            sourcePlan.setRppOriginalFlag(1);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("old")) {
                                            sourcePlan.setRppSplitPlanType("new");
                                            sourcePlan.setRppOriginalFlag(2);
                                        }
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(sourcePlan);
                                        changeList.add(sourcePlan.getRppId());
                                        List<BReportPlan> modelList = reportPlanDao.findBReportPlanByRppSrcId(sourcePlan.getRppId());
                                        for (BReportPlan model : modelList) {
                                            updateDescendantsRppNumByRppId(model);
                                        }
                                    }
                                }
                                sourcePlan = reportPlanDao.getById(sourceModel.getRppId());
                                if (null != sourceModel.getProvidedByPartyANum() && sourceModel.getProvidedByPartyANum() != 0 && sourceModel.getProvidedByPartyANum().equals(sourcePlan.getRppNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                            sourcePlan.setRppSplitPlanType("source");
                                            sourcePlan.setRppOriginalFlag(1);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("old")) {
                                            sourcePlan.setRppSplitPlanType("new");
                                            sourcePlan.setRppOriginalFlag(2);
                                        }
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        sourcePlan.setRppNum(sourceModel.getProvidedByPartyANum());
                                        sourcePlan.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(sourcePlan);
                                        sourcePlan.setRppUseStock(4);
                                        reportPlanDao.updateBReportPlanRppUseStockByRppId(sourcePlan);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(sourcePlan);
                                        changeList.add(sourcePlan.getRppId());
                                        List<BReportPlan> modelList = reportPlanDao.findBReportPlanByRppSrcId(sourcePlan.getRppId());
                                        for (BReportPlan model : modelList) {
                                            updateDescendantsRppNumByRppId(model);
                                        }
                                    }
                                } else if (null != providedByPartyA && sourceModel.getProvidedByPartyANum() != null && sourceModel.getProvidedByPartyANum() != 0 && !providedByPartyA.getRppNum().equals(sourceModel.getProvidedByPartyANum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (sourcePlan.getRppSplitPlanType().equals("source")) {
                                            sourcePlan.setRppSplitPlanType("oldSource");
                                            sourcePlan.setRppOriginalFlag(3);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("new")) {
                                            sourcePlan.setRppSplitPlanType("old");
                                            sourcePlan.setRppOriginalFlag(3);
                                        }

                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        providedByPartyA.setRppNum(sourceModel.getProvidedByPartyANum());
                                        providedByPartyA.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(providedByPartyA);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(providedByPartyA);
                                        changeList.add(providedByPartyA.getRppId());
                                    }
                                } else if (null != providedByPartyA && sourceModel.getProvidedByPartyANum() != 0 && !providedByPartyA.getRppNum().equals(sourceModel.getProvidedByPartyANum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (sourcePlan.getRppSplitPlanType().equals("source")) {
                                            sourcePlan.setRppSplitPlanType("oldSource");
                                            sourcePlan.setRppOriginalFlag(3);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("new")) {
                                            sourcePlan.setRppSplitPlanType("old");
                                            sourcePlan.setRppOriginalFlag(3);
                                        }

                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(providedByPartyA);
                                        providedByPartyA.setRppNum(sourceModel.getProvidedByPartyANum());
                                        providedByPartyA.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(providedByPartyA);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(providedByPartyA);
                                        changeList.add(providedByPartyA.getRppId());
                                        updateDescendantsRppNumByRppId(providedByPartyA);
                                    }
                                } else if (null != providedByPartyA && sourceModel.getProvidedByPartyANum() == 0 && !providedByPartyA.getRppNum().equals(sourceModel.getProvidedByPartyANum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        if (providedByPartyA.getRppSplitPlanType().equals("oldSource")) {
                                            providedByPartyA.setRppSplitPlanType("source");
                                            providedByPartyA.setRppOriginalFlag(1);
                                        } else if (providedByPartyA.getRppSplitPlanType().equals("old")) {
                                            providedByPartyA.setRppSplitPlanType("new");
                                            providedByPartyA.setRppOriginalFlag(2);
                                        }
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(providedByPartyA);
                                        providedByPartyA.setRppNum(0d);
                                        providedByPartyA.setRppChangeMark(1);
                                        reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(providedByPartyA);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(providedByPartyA);
                                        changeList.add(providedByPartyA.getRppId());
                                        updateDescendantsRppNumByRppId(providedByPartyA);
                                    }
                                } else if (null == providedByPartyA && sourceModel.getProvidedByPartyANum() != 0 && !sourceModel.getProvidedByPartyANum().equals(reportPlanDao.getById(sourceModel.getRppId()).getRppNum())) {
                                    providedByPartyA = reportPlanDao.getBReportPlanByIdAndRppUseStock(sourcePlan.getRppId(), 4);
                                    if (providedByPartyA == null) {
                                        if (sourcePlan.getRppSplitPlanType().equals("source") || sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                            sourcePlan.setRppOriginalFlag(3);
                                            sourcePlan.setRppSplitPlanType("oldSource");
                                        } else if (sourcePlan.getRppSplitPlanType().equals("old") || sourcePlan.getRppSplitPlanType().equals("new")) {
                                            sourcePlan.setRppOriginalFlag(3);
                                            sourcePlan.setRppSplitPlanType("old");
                                        }
                                        //更新原计划拆分标识与拆分身份
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        changeList.add(sourceModel.getRppId());
                                        sourcePlan.setRppNum(sourceModel.getProvidedByPartyANum());
                                        sourcePlan.setRppOriginalFlag(2);
                                        sourcePlan.setRppSplitPlanType("new");
                                        sourcePlan.setRppUseStock(4);
                                        if (sourcePlan.getRppAncestorsId() == null) {
                                            sourcePlan.setRppAncestorsId(sourcePlan.getRppId());
                                        }
                                        sourcePlan.setRppSrcId(sourceModel.getRppId());
                                        //插入新拆分的数据

                                        //插入新拆分的数据
                                        reportPlanDao.insertEntity(sourcePlan);
                                        changeList.add(sourcePlan.getRppId());
                                    }
                                } else if (null == providedByPartyA && sourceModel.getProvidedByPartyANum() != 0 && sourceModel.getProvidedByPartyANum().equals(reportPlanDao.getById(sourceModel.getRppId()).getRppNum())) {
                                    boolean isNotHaveSource = true;
                                    for (Integer change : changeList) {
                                        if (change.equals(sourceModel.getRppId())) {
                                            isNotHaveSource = false;
                                            break;
                                        }
                                    }
                                    if (isNotHaveSource) {
                                        BReportPlan bReportPlan = reportPlanDao.getById(sourceModel.getRppId());
                                        bReportPlan.setRppUseStock(4);
                                        //更新原计划提报类型
                                        reportPlanDao.updateBReportPlanRppUseStockByRppId(bReportPlan);
                                        if (sourcePlan.getRppSplitPlanType().equals("oldSource")) {
                                            sourcePlan.setRppSplitPlanType("source");
                                            sourcePlan.setRppOriginalFlag(1);
                                        } else if (sourcePlan.getRppSplitPlanType().equals("old")) {
                                            sourcePlan.setRppSplitPlanType("new");
                                            sourcePlan.setRppOriginalFlag(2);
                                        }
                                        reportPlanDao.updateBReportPlanRppOriginalFlagAndRppSplitPlanTypeByRppId(sourcePlan);
                                        reportPlanDao.updateBReportPlanRppRemarkSapCodeAndRppRemarkMatDesc(sourcePlan);
                                        changeList.add(sourcePlan.getRppId());
                                        List<BReportPlan> modelList = reportPlanDao.findBReportPlanByRppSrcId(sourcePlan.getRppId());
                                        for (BReportPlan model : modelList) {
                                            updateDescendantsRppNumByRppId(model);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //如果有错误数据则记录到错误日志表
        if (errorList.size() > 0) {
            callBack.onWarning(errorList);
        } else {
            callBack.onSuccess();
        }
    }

    /**
     * @name: onException
     * @description: 异常信息处理
     * @param: [exception 异常, context 内容]
     * @returnType: void
     * @author: 徐一贺
     * @coddingDate: 2020/8/21 11:13
     * @version: v1.0
     */
    @Override
    public void onException(Exception exception, AnalysisContext context) {
        exception.printStackTrace();
        exceptionIndexList.add(dataIndex);
        dataIndex++;
    }

    /**
     * @name: updateDescendantsRppNumByRppId
     * @description: 将其拆分出来的子孙级提报数量清零
     * @param: [model]
     * @returnType: void
     * @author: 徐一贺
     * @coddingDate: 2020/8/21 18:18
     * @version: v1.0
     **/
    private void updateDescendantsRppNumByRppId(BReportPlan model) {
        boolean isNotHave = true;
        for (Integer change : changeList) {
            if (change.equals(model.getRppId())) {
                isNotHave = false;
                break;
            }
        }
        if (isNotHave) {
            model.setRppNum(0d);
            model.setRppChangeMark(1);
            reportPlanDao.updateBReportPlanRppNumAndRppChangeMarkByRppId(model);
            changeList.add(model.getRppId());
        }
        List<BReportPlan> modelList = reportPlanDao.findBReportPlanByRppSrcId(model.getRppId());
        if (modelList.size() != 0) {
            for (BReportPlan obj : modelList) {
                updateDescendantsRppNumByRppId(obj);
            }
        }
    }

    /**
     * @name: checkNum
     * @description: 检测数量，主键ID
     * @param: [data]
     * @returnType: java.lang.String 错误信息
     * @author: 徐一贺
     * @coddingDate: 2020/8/20 15:19
     * @version: v1.0
     **/
    private String checkNum(ExportDeferralModel data) {
        String errorInfo = "";
        try {
            Double dataNum = reportPlanDao.getById(data.getRppId()).getRppNum();

            //验证ID错误
            if (null == data.getRppId() || reportPlanDao.getById(data.getRppId()) == null) {
                errorInfo += "ID不可修改！";
            }
            //验证数据是否发生变更
            String proofreadingResult = dataContentProofreading(data);
            if (!proofreadingResult.equals("")) {
                errorInfo += proofreadingResult;
                return errorInfo;
            }
            try {
                //验证正常提报数量是否是负数
                if (data.getNormalNum() != null && data.getNormalNum() < 0) {
                    errorInfo += "正常提报数量不能填负数！";
                }
                //验证正常提报数量是否大于计划提包数量
                if (data.getNormalNum() != null && data.getRppId() != null && reportPlanDao.getById(data.getRppId()) != null && data.getNormalNum() > dataNum) {
                    data.setRppNum(dataNum);
                    errorInfo += "正常提报数量不能大于本计划提报数量！";
                }
            } catch (Exception e) {
                //验证正常提报数量是否是数字
                errorInfo += "正常提报数量只能填数字！";
            }
            try {
                //验证使用库存数量是否是负数
                if (data.getUseStockNum() != null && data.getUseStockNum() < 0) {
                    errorInfo += "使用库存数量不能填负数！";
                }
                //验证使用库存数量是否大于计划提包数量
                if (data.getUseStockNum() != null && data.getRppId() != null && reportPlanDao.getById(data.getRppId()) != null && data.getUseStockNum() > dataNum) {
                    data.setRppNum(dataNum);
                    errorInfo += "使用库存数量不能大于本计划提报数量！";
                }
            } catch (Exception e) {
                //验证使用库存数量是否是数字
                errorInfo += "使用库存数量只能填数字！";
            }
            try {
                //验证暂缓数量是否是负数
                if (data.getPostponeNum() != null && data.getPostponeNum() < 0) {
                    errorInfo += "暂缓数量不能填负数！";
                }
                //验证暂缓数量是否大于计划提包数量
                if (data.getPostponeNum() != null && data.getRppId() != null && reportPlanDao.getById(data.getRppId()) != null && data.getPostponeNum() > dataNum) {
                    data.setRppNum(dataNum);
                    errorInfo += "暂缓数量不能大于本计划提报数量！";
                }
            } catch (Exception e) {
                //验证暂缓数量是否是数字
                errorInfo += "暂缓数量只能填数字！";
            }
            try {
                //验证甲供数量是否是负数
                if (data.getProvidedByPartyANum() != null && data.getProvidedByPartyANum() < 0) {
                    errorInfo += "甲供数量不能填负数！";
                }
                //验证甲供数量是否大于计划提包数量
                if (data.getProvidedByPartyANum() != null && data.getRppId() != null && reportPlanDao.getById(data.getRppId()) != null && data.getProvidedByPartyANum() > dataNum) {
                    data.setRppNum(dataNum);
                    errorInfo += "甲供数量不能大于本计划提报数量！";
                }
            } catch (Exception e) {
                //验证甲供数量是否是数字
                errorInfo += "甲供数量只能填数字！";
            }
            if (errorInfo.length() == 0) {
                if (data.getNormalNum() != null && data.getNormalNum() != 0 ||
                        data.getUseStockNum() != null && data.getUseStockNum() != 0 ||
                        data.getPostponeNum() != null && data.getPostponeNum() != 0 ||
                        data.getProvidedByPartyANum() != null && data.getProvidedByPartyANum() != 0) {
                    if (data.getNormalNum() == null) {
                        data.setNormalNum(0d);
                    }
                    if (data.getUseStockNum() == null) {
                        data.setUseStockNum(0d);
                    }
                    if (data.getPostponeNum() == null) {
                        data.setPostponeNum(0d);
                    }
                    if (data.getProvidedByPartyANum() == null) {
                        data.setProvidedByPartyANum(0d);
                    }
                    try {
                        Double totalNum = BigDecimalUtils.add(data.getNormalNum(), data.getUseStockNum());
                        totalNum = BigDecimalUtils.add(totalNum, data.getPostponeNum());
                        totalNum = BigDecimalUtils.add(totalNum, data.getProvidedByPartyANum());
                        if (!dataNum.equals(totalNum)) {
                            errorInfo += "正常提报数量、使用库存数量、暂缓数量、甲供数量的总和必须等于此条计划的数量！";
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return errorInfo;
    }

    /**
     * @name: dataContentProofreading
     * @description: 导入数据与数据库数据做比较，如果不同则返回false
     * @param: [database, compare]
     * @returnType: boolean
     * @author: 徐一贺
     * @coddingDate: 2020/8/21 11:18
     * @version: v1.0
     **/
    private String dataContentProofreading(ExportDeferralModel compare) {
        String errorInfo = "";
        ExportDeferralModel database = reportPlanDao.getExportDeferralModelById(compare.getRppId());
        //if ((compare.getRppPlanner() == null && database.getRppPlanner() != null) || !compare.getRppPlanner().equals(database.getRppPlanner())) {
        //    errorInfo += "此条计划与系统中计划的计划员不同！";
        //}
        //if ((compare.getRppMatDesc() == null && database.getRppMatDesc() != null) || !compare.getRppMatDesc().equals(database.getRppMatDesc())) {
        //    errorInfo += "此条计划与系统中计划的物料描述不同！";
        //}
        if ((compare.getRppNum() == null && database.getRppNum() != null) || !compare.getRppNum().equals(database.getRppNum())) {
            errorInfo += "此条计划与系统中计划的数量不同！";
        }
        //if ((compare.getRppUnit() == null && database.getRppUnit() != null) || !compare.getRppUnit().equals(database.getRppUnit())) {
        //    errorInfo += "此条计划与系统中计划的单位不同！";
        //}
        //if ((compare.getRppStandardNo() == null && database.getRppStandardNo() != null) || !compare.getRppStandardNo().equals(database.getRppStandardNo())) {
        //    errorInfo += "此条计划与系统中计划的标准号不同！";
        //}
        //if ((compare.getRppTypes() == null && database.getRppTypes() != null) || !compare.getRppTypes().equals(database.getRppTypes())) {
        //    errorInfo += "此条计划与系统中计划的采购类别不同！";
        //}
        //if ((compare.getRppBrand() == null && database.getRppBrand() != null) || !compare.getRppBrand().equals(database.getRppBrand())) {
        //    errorInfo += "此条计划与系统中计划的品牌不同！";
        //}
        //if ((compare.getRppProjectCode() == null && database.getRppProjectCode() != null) || !compare.getRppProjectCode().equals(database.getRppProjectCode())) {
        //    errorInfo += "此条计划与系统中计划的项目号不同！";
        //}
        //if ((compare.getRppProjectName() == null && database.getRppProjectName() != null) || !compare.getRppProjectName().equals(database.getRppProjectName())) {
        //    errorInfo += "此条计划与系统中计划的项目名称不同！";
        //}
        //if ((compare.getMcStatusName() == null && database.getMcStatusName() != null) || !compare.getMcStatusName().equals(database.getMcStatusName())) {
        //    errorInfo += "此条计划与系统中计划的物控状态不同！";
        //}
        //if ((compare.getRppUseStockText() == null && database.getRppUseStockText() != null) || !compare.getRppUseStockText().equals(database.getRppUseStockText())) {
        //    errorInfo += "此条计划与系统中计划的提报状态不同！";
        //}
        //if ((compare.getRppProgress() == null && database.getRppProgress() != null) || compare.getRppProgress() != null && database.getRppProgress() != null && !compare.getRppProgress().equals(database.getRppProgress())) {
        //    errorInfo += "此条计划与系统中计划的进度不同！";
        //}
        //if ((compare.getRppPlanNo() == null && database.getRppPlanNo() != null) || !compare.getRppPlanNo().equals(database.getRppPlanNo())) {
        //    errorInfo += "此条计划与系统中计划的计划号不同！";
        //}
        //if ((compare.getRppPlanStatus() == null && database.getRppPlanStatus() != null) || !compare.getRppPlanStatus().equals(database.getRppPlanStatus())) {
        //    errorInfo += "此条计划与系统中计划的计划状态不同！";
        //}
        BReportPlan bReportPlanModel = reportPlanDao.getById(compare.getRppId());
        if (bReportPlanModel.getRppUseStock() != null && bReportPlanModel.getRppUseStock().equals(1) && bReportPlanModel.getRppIsFlag() != null && bReportPlanModel.getRppIsFlag().equals(1)) {
            errorInfo += "此条计划已指定库存，不可操作！";
        }
        if (!bReportPlanModel.getRppPlanner().equals(ShiroUtils.getUser().getName())) {
            errorInfo += "此条计划不是你的，不可操作！";
        }
        if (!errorInfo.equals("")) {
            errorInfo += "请重新导出计划！";
        }
        return errorInfo;
    }

    /**
     * @name: recordErrorModel
     * @description: 记录错误信息
     * @param: [errorModel, errorInfo]
     * @returnType: void
     * @author: 徐一贺
     * @coddingDate: 2020/8/21 13:10
     * @version: v1.0
     **/
    private void recordErrorModel(ExportDeferralModel errorModel, String errorInfo) {
        ExportDeferralErrorLogModel recordModel = new ExportDeferralErrorLogModel();
        recordModel.setRppId(errorModel.getRppId());
        recordModel.setRppPlanner(errorModel.getRppPlanner());
        recordModel.setRppMatDesc(errorModel.getRppMatDesc());
        recordModel.setRppNum(errorModel.getRppNum());
        recordModel.setRppUnit(errorModel.getRppUnit());
        recordModel.setRppStandardNo(errorModel.getRppStandardNo());
        recordModel.setRppTypes(errorModel.getRppTypes());
        recordModel.setRppBrand(errorModel.getRppBrand());
        recordModel.setRppProjectCode(errorModel.getRppProjectCode());
        recordModel.setRppProjectName(errorModel.getRppProjectName());
        recordModel.setRppProjectStateStr(errorModel.getRppProjectStateStr());
        recordModel.setRppUseStockText(errorModel.getRppUseStockText());
        recordModel.setRppProgress(errorModel.getRppProgress());
        recordModel.setRppPlanNo(errorModel.getRppPlanNo());
        recordModel.setRppPlanStatus(errorModel.getRppPlanStatus());
        recordModel.setNormalNum(errorModel.getNormalNum());
        recordModel.setUseStockNum(errorModel.getUseStockNum());
        recordModel.setPostponeNum(errorModel.getPostponeNum());
        recordModel.setProvidedByPartyANum(errorModel.getProvidedByPartyANum());
        recordModel.setErrorInfo(errorInfo);
        recordModel.setErrorDate(operationTime);
        recordModel.setErrorUid(operatorId);
        errorList.add(recordModel);
    }

}